package com.cr.sync.service.impl;

import com.cr.common.utils.StringUtils;
import com.cr.sync.domain.TbSyncMainBodyInfo;
import com.cr.sync.domain.dto.LsdCountParam;
import com.cr.sync.domain.vo.CountVo;
import com.cr.sync.domain.vo.LsdIndexMainBody;
import com.cr.sync.domain.vo.LsdMainBodyCountResult;
import com.cr.sync.domain.vo.LsdMainBodyInfo;
import com.cr.sync.mapper.TbSyncMainBodyInfoMapper;
import com.cr.sync.service.ITbSyncMainBodyInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 远程数据-主体信息Service业务层处理
 * 
 * @author tang
 * @date 2025-08-29
 */
@Service
public class TbSyncMainBodyInfoServiceImpl implements ITbSyncMainBodyInfoService 
{
    @Resource
    private TbSyncMainBodyInfoMapper tbSyncMainBodyInfoMapper;

    @Override
    public int insertBatch(List<TbSyncMainBodyInfo> list) {
//        tbSyncMainBodyInfoMapper.deleteBatch(list);
        tbSyncMainBodyInfoMapper.insertBatch(list);
        return 1;
    }

    @Override
    public LsdMainBodyCountResult getMainBodyCount(LsdCountParam param) {
        String area = param.getArea();
        // 1、主体属性数量统计
        List<CountVo> attributeCount = tbSyncMainBodyInfoMapper.selectSubjectAttributeCount(area);

        // 2、主体行业数量统计
        List<CountVo> businessCount = tbSyncMainBodyInfoMapper.selectbusinessCount(area);

        // 3、查询主体信息列表
        Integer page = param.getPage();
        Integer pageSize = param.getPageSize();
        if(page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        // 分页查询
        PageHelper.startPage(page, pageSize);
        List<LsdMainBodyInfo> lsdMainBodyInfos = tbSyncMainBodyInfoMapper.selectLsdMainBodyInfoList(area);
        PageInfo<LsdMainBodyInfo> pageInfo = new PageInfo<>(lsdMainBodyInfos);
        PageHelper.clearPage();

        // 组装返回参数
        LsdMainBodyCountResult result = new LsdMainBodyCountResult();
        result.setAttributeCount(attributeCount);
        result.setBusinessCount(getBusinessCountResult(businessCount));
        result.setMainBodyList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    private List<CountVo> getBusinessCountResult(List<CountVo> businessCount) {
        // 主体行业数量
        // 1.种植业
        int totalTypeOne = 0;
        // 2.畜牧业
        int totalTypeTwo = 0;
        // 3.水产业
        int totalTypeThree = 0;
        // 4.其他行业
        int totalTypeFour = 0;
        if(businessCount != null && !businessCount.isEmpty()) {
            for (CountVo entity : businessCount) {
                String label = entity.getLabel();
                if(StringUtils.isNotBlank(label) && label.contains("1")) {
                    totalTypeOne += entity.getValue();
                }
                if(StringUtils.isNotBlank(label) && label.contains("2")) {
                    totalTypeTwo += entity.getValue();
                }
                if(StringUtils.isNotBlank(label) && label.contains("3")) {
                    totalTypeThree += entity.getValue();
                }
                if(StringUtils.isNotBlank(label) && label.contains("4")) {
                    totalTypeFour += entity.getValue();
                }
            }
        }

        List<CountVo> businessCountResult = new ArrayList<>();
        businessCountResult.add(new CountVo("1", totalTypeOne));
        businessCountResult.add(new CountVo("2", totalTypeTwo));
        businessCountResult.add(new CountVo("3", totalTypeThree));
        businessCountResult.add(new CountVo("4", totalTypeFour));
        return businessCountResult;
    }

    @Override
    public LsdIndexMainBody getIndexMainBodyCount(String area) {
        LsdIndexMainBody result = new LsdIndexMainBody();

        List<CountVo> attributeCount = tbSyncMainBodyInfoMapper.selectSubjectAttributeCount(area);
        if (attributeCount != null && !attributeCount.isEmpty()) {
            for (CountVo entity : attributeCount) {
                // 主体属性(1 生产主体 2 屠宰场 3 生鲜乳企业 4 投入品经营主体 5 加工企业)
                String label = entity.getLabel();
                if("1".equals(label)) {
                    result.setSc(entity.getValue());
                }
                if("2".equals(label)) {
                    result.setTz(entity.getValue());
                }
                if("5".equals(label)) {
                    result.setJg(entity.getValue());
                }
            }
        }

        return result;
    }

}
